<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>generator ex1</title>
</head>
<body>
    

    <script>
        
        {
            function* fn (x) {
                yield x+1;
                yield x+2;
                yield x+3;
            }

            let f = fn(1);
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
        }

        {
            function* gn () {
                yield function () {
                    console.log('1111');
                };
                yield function () {
                    console.log('2222');
                };
                yield function () {
                    console.log('3333');
                };;
            } 

            let g = gn();
            let gg = g.next().value;
            console.log(gg());
            console.log(gg());
            console.log(gg());
            console.log(gg());
            console.log(gg());
        }

        {
            function* fib(max) {
                var
                    t,
                    a = 0,
                    b = 1,
                    n = 0;
                while (n < max) {
                    yield a;
                    console.log('a',a);
                    [a, b] = [b, a + b];
                    n ++;
                }
                return;
            }

            let f = fib(5);
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
            console.log(f.next());
        }

        {
            function* jn () {
                let a = 0;
                let b = 0;
                yield a;
                b = a + 1;
                return b;
            }

            let j = jn();
            console.log(j);
            console.log(j.next());
            console.log(j.next());
            console.log(j.next());
            console.log(j.next());
            console.log(j.next());
        }

        {
            function* kn (count) {
                while (count) {
                    yield 1;
                    yield 2;
                    yield 3;
                }
            }

            let k = kn(1);

            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
            console.log(k.next());
        }

        {
            function fn () {
                return new Promise ((resolve,reject)=>{
                    setTimeout(() => {
                        resolve('123');
                    }, 1000);
                });
            }

            function* gn () {
                yield fn();
                yield fn();
            }

            console.log(gn().next());
            console.log(gn().next());
            console.log(gn().next());
            console.log(gn().next());
            console.log(gn().next());
        }
    
    </script>
</body>
</html>